package com.hyg.oa.common;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class SecurityInterceptor extends HandlerInterceptorAdapter  {

	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		System.out.println("afterCompletion");
		
	}

	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		System.out.println("postHandle");
		 
	}

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		System.out.println("preHandle");
		
		String path = request.getServletPath();
		
		//是否是登陆请求，放行
		if(path.equals("/user/login.do")) {
			return true;
		}
		
		HttpSession session =  request.getSession();
		if(session.getAttribute("username") != null) {
			return true;
		} else {
			
			response.sendRedirect(request.getContextPath() + "/jsp/login.jsp");  
			
			return false;
		}
		
	}

}
